home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Freeware / Programare / highlight / highlight-W32GUI-2.2-10b-Setup.exe / {app} / src / latexgenerator.cpp < prev    next >
C/C++ Source or Header  |  2005-03-31  |  8KB  |  327 lines

  1. /***************************************************************************
  2.                           LatexCode.cpp  -  description
  3.                              -------------------
  4.     begin                : Mit Jul 24 2002
  5.     copyright            : (C) 2002 by AndrΘ Simon
  6.     email                : andre.simon1@gmx.de
  7.  ***************************************************************************/
  8.  
  9. /***************************************************************************
  10.  *                                                                         *
  11.  *   This program is free software; you can redistribute it and/or modify  *
  12.  *   it under the terms of the GNU General Public License as published by  *
  13.  *   the Free Software Foundation; either version 2 of the License, or     *
  14.  *   (at your option) any later version.                                   *
  15.  *                                                                         *
  16.  ***************************************************************************/
  17.  
  18. #include "latexgenerator.h"
  19.  
  20. namespace highlight {
  21.  
  22. LatexGenerator::LatexGenerator(const string &colourTheme,
  23.                                bool replQuotes)
  24.     : CodeGenerator(colourTheme),
  25.       replaceQuotes(replQuotes)
  26. {
  27.   styleTagOpen.push_back( "\\hlstd{");
  28.   styleTagOpen.push_back( "\\hlstr{");
  29.   styleTagOpen.push_back( "\\hlnum{");
  30.   styleTagOpen.push_back( "\\hlslc{");
  31.   styleTagOpen.push_back( "\\hlcom{");
  32.   styleTagOpen.push_back( "\\hlesc{");
  33.   styleTagOpen.push_back( "\\hldir{");
  34.   styleTagOpen.push_back( "\\hldstr{");
  35.   styleTagOpen.push_back( "\\hlline{");
  36.   styleTagOpen.push_back( "\\hlsym{");
  37.  
  38.   for (int i=0;i<NUMBER_BUILTIN_STYLES; i++){
  39.    styleTagClose.push_back( "}");
  40.   }
  41.  
  42.   // avoid "Underfull \hbox (badness 10000)" warnings
  43.   newLineTag = "\\\\\n";
  44.   longLineTag = "\\hspace*{\\fill}" + newLineTag;
  45.  
  46.   spacer = "\\ ";
  47.  
  48.   maskWs=true;
  49.   maskWsBegin = "\\hlstd{";
  50.   maskWsEnd = "}";
  51.  
  52.   excludeWs=true;
  53.  
  54.   styleCommentOpen="%";
  55. }
  56.  
  57. LatexGenerator::LatexGenerator()
  58. {}
  59. LatexGenerator::~LatexGenerator()
  60. {}
  61.  
  62. string LatexGenerator::formatStyleAttributes(const string & elemName,
  63.                                              const ElementStyle &elem)
  64. {
  65.   ostringstream s;
  66.   s  << "\\newcommand{\\hl"
  67.      << elemName
  68.      << "}[1]{\\textcolor[rgb]{"
  69.      << elem.getColour().getLatexRedValue() << ","
  70.      << elem.getColour().getLatexGreenValue() << ","
  71.      << elem.getColour().getLatexBlueValue()
  72.      << "}{";
  73.  
  74.   if (elem.isBold())
  75.     s << "\\bf{";
  76.   if (elem.isItalic())
  77.     s << "\\it{";
  78.  
  79.   s  <<"#1";
  80.  
  81.   if (elem.isBold())
  82.     s << "}";
  83.   if (elem.isItalic())
  84.     s << "}";
  85.  
  86.   s  <<"}}\n";
  87.   return s.str();
  88. }
  89.  
  90. void LatexGenerator::printBody()
  91. {
  92.   *out << "\\noindent\n"
  93.        << "\\ttfamily\n";
  94.  
  95.   processRootState();
  96.  
  97.   *out << "\\mbox{}\n"
  98.        << "\n\\normalfont\n";
  99. }
  100.  
  101. string LatexGenerator::getHeader(const string & title)
  102. {
  103.   ostringstream os;
  104.   os << "\\documentclass{article}\n"
  105.      << "\\usepackage{color}\n"
  106.      << "\\usepackage{alltt}\n";
  107.  
  108.   if (langInfo.getSyntaxHighlight()) {
  109.     if (includeStyleDef) {
  110.       os << "\n"<<getStyleDefinition();
  111.       os << CodeGenerator::readUserStyleDef();
  112.     }  else  {
  113.       os << "\n\\input {"
  114.          << getStyleOutputPath()
  115.          << "}\n";
  116.     }
  117.   }
  118.  
  119.   os << "\n\\title{" << title << "}\n"
  120.      << "\\begin{document}\n"
  121.      << "\\pagecolor{bgcolor}\n";
  122.   return os.str();
  123. }
  124.  
  125. string LatexGenerator::getFooter()
  126. {
  127.   ostringstream os;
  128.   os << "\\end {document}\n"
  129.      << "(* LaTeX generated by highlight "
  130.      << HIGHLIGHT_VERSION
  131.      << ", "
  132.      << HIGHLIGHT_URL
  133.      << " *)\n";
  134.   return os.str();
  135. }
  136.  
  137. string LatexGenerator::getNewLine(){
  138.     return (showLineNumbers)? newLineTag:longLineTag;
  139. }
  140.  
  141. string LatexGenerator::maskCharacter(unsigned char c)
  142. {
  143.   switch (c)
  144.     {
  145.     case '<' :
  146.       return "$<$";
  147.       break;
  148.     case '>' :
  149.       return "$>$";
  150.       break;
  151.     case '{':
  152.     case '}':
  153.     case '&':
  154.     case '$':
  155.     case '#':
  156.     case '%':
  157.       {
  158.        string m;
  159.        m  ="\\";
  160.        m += c;
  161.        return m;
  162.       }
  163.       break;
  164.     case '\"':
  165.       return (fragmentOutput && replaceQuotes)?"\\dq{}":"\"";
  166.       break;
  167.     case '_':
  168.       return "\\textunderscore ";
  169.       break;
  170.     case '^':
  171.       return "\\textasciicircum ";
  172.       break;
  173.     case '\\':
  174.       return "$\\backslash$";
  175.       break;
  176.     case '~':
  177.       return "$\\sim$";
  178.       break;
  179.     case '|':
  180.       return "\\textbar ";
  181.       break;
  182.     // avoid latex compilation failure if [ or * follows a line break (\\)
  183.     case '*':
  184.     case '[':
  185.     case ']':
  186.     // avoid "merging" of consecutive '-' chars when included in bold font ( \bf )
  187.     case '-':
  188.       {
  189.        string m;
  190.        m= "{";
  191.        m+= c;
  192.        m+= "}";
  193.        return m;
  194.       }
  195.       break;
  196.     case ' ':
  197.       return spacer;
  198.       break;
  199.     case AUML_LC:
  200.       return "\\\"a";
  201.       break;
  202.     case OUML_LC:
  203.       return "\\\"o";
  204.       break;
  205.     case UUML_LC:
  206.       return "\\\"u";
  207.       break;
  208.     case AUML_UC:
  209.       return "\\\"A";
  210.       break;
  211.     case OUML_UC:
  212.       return "\\\"O";
  213.       break;
  214.     case UUML_UC:
  215.       return "\\\"U";
  216.       break;
  217.     case AACUTE_LC:
  218.       return "\\'a";
  219.       break;
  220.     case EACUTE_LC:
  221.       return "\\'e";
  222.       break;
  223.     case OACUTE_LC:
  224.       return "\\'o";
  225.       break;
  226.     case UACUTE_LC:
  227.       return "\\'u";
  228.       break;
  229.     case AGRAVE_LC:
  230.       return "\\`a";
  231.       break;
  232.     case EGRAVE_LC:
  233.       return "\\`e";
  234.       break;
  235.     case OGRAVE_LC:
  236.       return "\\`o";
  237.       break;
  238.     case UGRAVE_LC:
  239.       return "\\`u";
  240.       break;
  241.     case AACUTE_UC:
  242.       return "\\'A";
  243.       break;
  244.     case EACUTE_UC:
  245.       return "\\'E";
  246.       break;
  247.     case OACUTE_UC:
  248.       return "\\'O";
  249.       break;
  250.     case UACUTE_UC:
  251.       return "\\'U";
  252.       break;
  253.     case AGRAVE_UC:
  254.       return "\\`A";
  255.       break;
  256.     case EGRAVE_UC:
  257.       return "\\`E";
  258.       break;
  259.     case UGRAVE_UC:
  260.       return "\\`O";
  261.       break;
  262.     case OGRAVE_UC:
  263.       return "\\`U";
  264.       break;
  265.     case SZLIG:
  266.       return "\\ss ";
  267.       break;
  268.  /*    #ifndef _WIN32
  269.  // skip  first byte of multibyte chracters
  270.     case 195:
  271.       return string("");
  272.       break;
  273. #endif*/
  274.  
  275.     default :
  276.       {
  277.       string m;
  278.       return m+=c;
  279.      }
  280.     }
  281. }
  282.  
  283. string LatexGenerator::getMatchingOpenTag(unsigned int styleID){
  284.   return "\\hl"+langInfo.getKeywordClasses()[styleID]+"{";
  285.  }
  286.  
  287. string LatexGenerator::getMatchingCloseTag(unsigned int styleID){
  288.   return "}";
  289. }
  290.  
  291.  
  292. string LatexGenerator::getStyleDefinition()
  293. {
  294.     if (styleDefinitionCache.empty()){
  295.         ostringstream os;
  296.         os << formatStyleAttributes("std", docStyle.getDefaultStyle());
  297.         os << formatStyleAttributes("num", docStyle.getNumberStyle());
  298.         os << formatStyleAttributes("esc", docStyle.getEscapeCharStyle());
  299.         os << formatStyleAttributes("str", docStyle.getStringStyle());
  300.         os << formatStyleAttributes("dstr", docStyle.getDirectiveStringStyle());
  301.         os << formatStyleAttributes("slc", docStyle.getSingleLineCommentStyle());
  302.         os << formatStyleAttributes("com", docStyle.getCommentStyle());
  303.         os << formatStyleAttributes("dir", docStyle.getDirectiveStyle());
  304.         os << formatStyleAttributes("sym", docStyle.getSymbolStyle());
  305.         os << formatStyleAttributes("line", docStyle.getLineStyle());
  306.  
  307.         KeywordStyles styles = docStyle.getKeywordStyles();
  308.         for (KSIterator it=styles.begin(); it!=styles.end(); it++){
  309.             os << formatStyleAttributes(it->first, *(it->second));
  310.         }
  311.         os << "\\definecolor{bgcolor}{rgb}{"
  312.                 << docStyle.getBgColour().getLatexRedValue() << ","
  313.                 << docStyle.getBgColour().getLatexGreenValue() << ","
  314.                 << docStyle.getBgColour().getLatexBlueValue()
  315.                 << "}\n";
  316.         os << "\\oddsidemargin -3mm\n\\textwidth 165,2truemm\n"
  317.                 << "\\topmargin 0truept\n\\headheight 0truept\n"
  318.                 << "\\headsep 0truept\n\\textheight 230truemm\n";
  319.  
  320.         styleDefinitionCache=os.str();
  321.     }
  322.     return styleDefinitionCache;
  323. }
  324.  
  325.  
  326. }
  327.